{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 购物车项目训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 项目要求：\n",
    "1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "2. 让用户输入商品编号进行商品的购买。\n",
    "3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝余额8800\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号:0\n",
      "你已将商品华为p40添加到购物车，你的支付宝余额是4000\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号:6\n",
      "你已将商品head first python添加到购物车，你的支付宝余额是3872\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号:q\n",
      "('华为p40', 4800)\n",
      "('head first python', 128)\n",
      "你的支付宝余额是3872\n"
     ]
    }
   ],
   "source": [
    "# Elaine\n",
    "\n",
    "product_list = [\n",
    "    ('华为p40',4800),\n",
    "    ('mac pro',16000),\n",
    "    ('milk tea', 18),\n",
    "    ('crabs', 88),\n",
    "    ('bike', 800),\n",
    "    ('watch', 10000),\n",
    "    ('head first python',128),\n",
    "    ('book', 100)\n",
    "]\n",
    "shopping_car = []\n",
    "zhifubao = input('请输入支付宝余额')\n",
    "if zhifubao.isdigit(): #【isdigit】确认你输入的是不是全部是数字,y-true,n-false\n",
    "    zhifubao = int(zhifubao)\n",
    "    while True:\n",
    "        for item in product_list:\n",
    "            print(product_list.index(item),item)\n",
    "        choice = input('请输入商品编号:')\n",
    "        if choice.isdigit():\n",
    "            choice = int(choice)\n",
    "            if choice >=0 and choice <=7:\n",
    "                if zhifubao >= product_list[choice][1]:\n",
    "                    zhifubao = zhifubao - product_list[choice][1]\n",
    "                    shopping_car.append(product_list[choice])\n",
    "                    print(\"你已将商品%s添加到购物车，你的支付宝余额是%s\"% (product_list[choice][0],zhifubao))\n",
    "                else:\n",
    "                    print(\"你的支付宝余额不足，请选择其它商品\")\n",
    "            else:\n",
    "                print(\"所选商品不在列表\")\n",
    "        elif choice == \"q\":\n",
    "            for item in shopping_car:\n",
    "                print(item)\n",
    "            print(\"你的支付宝余额是%s\"% zhifubao)\n",
    "            break\n",
    "        else:\n",
    "            print(\"请你输入商品编号\")\n",
    "else:\n",
    "    print(\"请输入的余额为数字\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
